<?php 

require_once __DIR__ . '/../conf/website.conf.php';
require_once __DIR__ . '/func.php';
require_once __DIR__ . '/Database.class.php';
require_once __DIR__ . '/Workerman/Autoloader.php';
use Workerman\Worker;

$ws = new Worker('websocket://0.0.0.0:8484');

$ws->count=2;

$user = array();

$ws->onWorkerStart = function($connection) use(&$website,&$user)
{
  $website['class']['db'] = new Database($website);

  $connection->onWebSocketConnect = function($connection , $http_header) use(&$website,&$user)
  {
    echo "new connection from ip " . $connection->getRemoteIp() . "\n";
    
    $connection->onMessage = function($connection, $data) use(&$website,&$user)
    {   

   


        // $json {act:admin-type-select,xxx:yy}
        if($json = json_decode($data,1)){
          $back = fn_act($website,$json);

          $connection->send(json_encode($back));
        }else{
          $connection->send(json_encode(array('status'=>'error','msg'=>'非法数据')));
        }

        
    };
  };

};


function fn_act(&$website,$data){
  $file = $website['path']['socket'].'/'.str_replace('-', '/', $data['act']).'.php'; 
  include_once($file);

  return $json;

}







// 运行worker
Worker::runAll();